package com.example.springboot.service.impl;

import com.example.springboot.utils.Md5Util;
import com.example.springboot.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springboot.pojo.User;
import com.example.springboot.service.UserService;
import com.example.springboot.mapper.UserMapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public void register(String username, String password, String userphone, String idCard) {
        String md5String = Md5Util.getMD5String(password);
        userMapper.register(username, md5String, userphone, idCard);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);

    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public PageInfo<User> selectPage( Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.findAll();
        return PageInfo.of(list);
    }
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.delete(id);
        }

    }
    @Override
    public void add(User user) {
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        userMapper.add(user);
    }

}
